<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<style>
    .datagrid-header-row {
        background-color: #4dded8;
    }

    a:link {color: #5c63ff; text-decoration:none;}/* 未访问：蓝色、无下划线*/
    /* a:active {color: red; } 激活：红色
    a:visited {color:purple;text-decoration:none;} 已访问：紫色、无下划线*/
    a:hover {color: red; text-decoration:underline;} /*鼠标移近：红色、下划线*/
</style>
<div class="goods-table">
    <label for="code">编号:</label>
    <input type="text" name="code" id="code"/>
    <label for="content">名称:</label>
    <input type="text" name="content" id="content"/>
    <label for="categoryId">分类:</label>
    <input id="categoryId" class="easyui-combobox" name="categoryId"
           data-options="valueField:'id',textField:'name',method:'get',url:'api/goods/category/findAll'" />
    <em><a id="searchGood" href="#" class="easyui-linkbutton"data-options="iconCls:'icon-search'">查询</a></em>
    <i><a id="addGood" href="#" class="easyui-linkbutton"data-options="iconCls:'icon-add'">添加</a></i>
    <b><a id="reloadGood" href="#" class="easyui-linkbutton"data-options="iconCls:'icon-reload'">刷新</a></b>
    <table id="goodsGrid"></table>
</div>
<div id="goodsDialog"></div>
<div id="goodsAddDialog"></div>
<div id="addToOrderDialog"></div>
<input type="hidden" name="goods" id="goods">
<script>
    $(function () {
    
        $('#goodsGrid').datagrid({
            height: ($(window).height()*0.9),
            url:'api/goods/good/list',
            method: 'POST',
            striped: true,
            fitColumns: true,
            singleSelect: true,
            // rowNumbers: true,
            pagination: true,  //true代表显示分页控件 ，而且传到后台的参数中会自动添加 page 和 row 两参数，后台直接就可以接收到
            //  nowrap: false,
            //   showFooter: true,
            pageSize: 20,//每页显示的记录条数，默认为10
            pageList: [10,20,30,50,70],//可以设置每页记录条数的列表
            columns: [[
                { field: 'code', title: '编号', width: 80, align: 'center' },
                { field: 'name', title: '名称', width: 100, align: 'center' },
                { field: 'categoryId', title: '分类', width: 150, align: 'center',
                    formatter:function(value,rowData,index){
                        if(rowData.categoryId != null){
                            return rowData.categoryName;
                        }
                    }},
                { field: 'salePrice', title: '零售价', width: 50, align: 'center' },
                {field:'balanceCount', title: '库存',width: 50, align: 'center'},
                { field: 'spec', title: '商品规格', width: 100, align: 'center' },
                { field: 'unitId', title: '单位', width: 50, align: 'center',
                    formatter:function(value,rowData,index){
                        if(rowData.unitId != null){
                            return rowData.unitName;
                        }
                    }},
                { field: 'purchasePrice', title: '进价', width: 50, align: 'center' },
                { field: 'act', title: '操作', width: 50, align: 'center' ,
                    formatter:function(value,rowData,index){
                        return '<a addToOrder href="javascript:;">加入</a>'
                            +'<a del id="' + rowData.id + '" href="javascript:;" >删除</a> ';
                    }
                }
            ]],
            loadFilter: function (data) {
                if(data != null ){
                    return data.data;
                }
            }, onDblClickRow :function(rowIndex,rowData){
                var jsonObj=$('#goodsGrid').datagrid('getSelected');
                $("#goods").val(JSON.stringify(jsonObj));
                $('#goodsDialog').dialog({
                    title: '商品编辑',
                    width: 800,
                    height: 500,
                    closed: false,
                    cache: false,
                    href: 'page/goods-details',
                    modal: true
                });
            }
        });
    	
    });
    $('.goods-table').on('click', 'em', function (e) {
        e.preventDefault();
        var code=$('#code').val();
        var name=$('input[name="content"]').val();
        var categoryId=$('#categoryId').combobox('getText');
        if(categoryId === ''){
            categoryId=$('#categoryId').val();
        }
        e.stopPropagation();
        $('#goodsGrid').datagrid({
            url:'api/goods/good/list',
            method: 'POST',
            queryParams:{code:code,name:name,categoryName:categoryId,page:"1",rows:"10"}
        });
    }).on('click','b',function (e) {
        e.preventDefault();
        e.stopPropagation();
        $('#code').val('');
        $('input[name="content"]').val('');
        $('#categoryId').combobox('setValue','');
        $('#goodsGrid').datagrid({
            url:'api/goods/good/list',
            queryParams:{page:"1",rows:"10"}
        });
    }).on('click','i',function(e){
        e.preventDefault();
        e.stopPropagation();
        $('#goodsAddDialog').dialog({
            title: '添加商品',
            width: 800,
            height: 500,
            closed: false,
            cache: false,
            href: 'page/goods-add',
            modal: true
        });
    }).on('click','a[del]',function(e){
        e.preventDefault();
        e.stopPropagation();
        var $this=$(this);
        var msg = "删除后无法恢复，您真的确定要删除吗？";
        if (confirm(msg)==true){
            $.ajax({
                type: "delete",
                url: "api/goods/good/"+$this.attr('id'),
                success: function (data, status) {
                    console.info(data);
                    if(data){
                        $('#goodsGrid').datagrid('reload');
                    }
                },
                error:function(data){
                    $.messager.alert('提示信息',data.responseJSON.message, 'warning');
                }
            });
        }else{
            return false;
        }
    }).on('click','a[addToOrder]',function (e) {
        e.preventDefault();
        e.stopPropagation();
        var jsonObj=$('#goodsGrid').datagrid('getSelected');
        $("#goods").val(JSON.stringify(jsonObj));
        $('#addToOrderDialog').dialog({
            title: '菜单',
            width: 720,
            height: 300,
            closed: false,
            cache: false,
            href: 'page/goods-addToOrder',
            modal: true
        });
    });




</script>
